<?php
include "templates/header.php";

if (isset($_POST['update_products'])) {
    
    foreach ($_POST['data'] as $key => $product) {
        if ($_FILES['data']['name'][$key]['new_picture']) {
            $blacklist = array(".php", ".phtml", ".php3", ".php4");
            foreach ($blacklist as $item) {
                if (preg_match("/$item\$/i", $_FILES['data']['name'][$key]['new_picture'])) {
                    echo "We do not allow uploading PHP files\n";
                    exit;
                }
            }
            $g_id = $_POST['add_photo_category'];
            $uploaddir = 'img/' . $key . '/'; // Relative path under webroot
            if(!is_dir(ROOT_PATH . $uploaddir)){
                mkdir(ROOT_PATH . $uploaddir);
            }
            $description = $_POST['data'][$key]['new_picture_description'] ? $_POST['data'][$key]['new_picture_description'] : "";
            $uploadfile = $uploaddir . md5(time() . $_FILES['data']['name'][$key]['new_picture'] . rand(0, 50000)) . "." . basename($_FILES['data']['type'][$key]['new_picture']);
            move_uploaded_file($_FILES['data']['tmp_name'][$key]['new_picture'], ROOT_PATH . $uploadfile);
            mysql_insert("img", array("path" => $uploadfile, "prod_id" => $key, "description" => $description));

        }
        if (!isset($product['active'])) {
            $product['active'] = "0";
        }
        unset($product['new_picture_description']);
        foreach ($product as $k => $value) {
            $product[$k] = addslashes($value);
        }
        mysql_update('product', $product, $key);
    }
    if($_POST['img']){
        foreach ($_POST['img'] as $key => $img) {
            if (!isset($img['logo'])) {
                $img['logo'] = "0";
            }
            mysql_update('img', $img, $key);
        }
    }
}

    $products = mysql_select_array("SELECT * FROM `product` ORDER BY `title` ASC");
    foreach ($products as $key => $product) {
        $products[$key]['images'] = mysql_select_array("SELECT * FROM `img` WHERE `prod_id`=?", $product['id']);
    }
    $categories = mysql_select_array("SELECT * FROM `category` ORDER BY `title` ASC");
?>
<script>
$(function(){
    $(".product_title_row").click(function(){
        $(this).closest('tr').next('tr').slideToggle(0);
        if($(this).html()=="Развернуть"){
            $(this).html("Свернуть");
        } else{
            $(this).html("Развернуть");
        }
        
    });
    $('.delete_product').click(function(){
        if(confirm("Удалить продукт \"" + $("#title_" + $(this).attr("id")).val() + "\"?")){
            $.post("../source/ajax.php?action=delete_product", {
                id: $(this).attr("id")
            });
            $(this).closest('tr').hide();
        }
        
    });
});
</script>
    <form  enctype="multipart/form-data" action="productslisting.php" method="post">
        <input type="hidden" name="update_products" value="1" />
        <table border="1" cellspacing="0" style="width: 100%;">
            <tr>
                <th>Наименование</th>
                <th>Показать</th>
                <th>&nbsp;</th>
            </tr>
                <? foreach ($products as $key => $product): ?>
                <tr>
                    <td style="width: 80%;"><span class="product_title_row">Развернуть</span><input type="text" id="title_<?= $product['id']; ?>" style="width: 80%;" name="data[<?= $product['id']; ?>][title]" value="<?= stripslashes($product['title']); ?>" /></td>
                    <td style="width: 80px; text-align: center;"><input type="checkbox" name="data[<?= $product['id']; ?>][active]" value="1" <? if ($product['active']): ?>checked="checked"<? endif; ?> /></td>
                    <td style="width: 80px; text-align: center;"><span id="<?= $product['id']; ?>" class="delete_product">Удалить</span></td>
                </tr>
                <tr style="display:none;">
                    <td colspan="3">
                        <table style="width: 100%;">
                            <tr>
                                <th>Описание</th>
                                <th>Краткое описание</th>
                                <th>Ключевые слова</th>
                            </tr>
                            <tr>
                                <tr colspan="3">Категория:
                                <select name="data[<?= $product['id']; ?>][category_id]" style="width: 300px;">
                            <option value="0">Выберите категорию</option>
                                <? foreach ($categories as $key => $category): ?>
                                    <option <? if ($product['category_id'] == $category['id']): ?>selected="selected"<? endif; ?> value="<?= $category['id']; ?>"><?= trim($category['title']); ?></option>
                                <? endforeach; ?>
                        </select>
                                </tr>
                            </tr>
                            <tr>
                                <td><textarea name="data[<?= $product['id']; ?>][description]" style="width: 100%; min-height: 150px;"><?= stripslashes($product['description']); ?></textarea></td>
                                <td><textarea name="data[<?= $product['id']; ?>][summary]" style="width: 100%; min-height: 150px;"><?= stripslashes($product['summary']); ?></textarea></td>
                                <td><textarea name="data[<?= $product['id']; ?>][keywords]" style="width: 100%; min-height: 150px;"><?= stripslashes($product['keywords']); ?></textarea></td>
                            </tr>
                            <tr>
                                <td colspan="3">
                                    <?if($product['images']):?>
                                        <?foreach($product['images'] as $img):?>
                                        <div id="img_container_<?=$img['id'];?>" style="width: 250px; height: 150px; float: left; text-align: center;">
                                            <input class="img_prod_<?=$product['id'];?>" id="img_<?=$img['id'];?>" onclick="checklogo('<?=$img['id'];?>', '<?=$product['id'];?>')" type="checkbox" name="img[<?=$img['id'];?>][logo]" <?if($img['logo']):?>checked="checked"<?endif;?> value="1" />
                                            <img src="<?=BASE_URL.$img['path'];?>" style="height: 120px;" />
                                            <span onclick="delete_image('<?=$img['id'];?>');">[X]</span><br />
                                            <input type="text" name="img[<?=$img['id'];?>][description]" value="<?=$img['description'];?>" style="width: 100%; height: 20px;" />
                                        </div>
                                        <?endforeach;?>
                                    <?endif;?>
                                    <div style="width: 250px; height: 150px; float: left;">
                                        <br />Загрузить новое изображение.<br />
                                        <input type="file" name="data[<?= $product['id']; ?>][new_picture]"  /><br />
                                        Описание:<br />
                                        <input type="text" name="data[<?= $product['id']; ?>][new_picture_description]"  />
                                    </div>
                                </td>
                            </tr>
                        </table>
                    </td>
                    
                </tr>

            <? endforeach; ?>
        </table>
        <input type="submit" value="Обновить">
    </form>
<script>
    function checklogo($imgid, $prodid){
        $(".img_prod_"+$prodid).attr('checked', false);
        $("#img_"+$imgid).attr('checked', true);
    }
    function delete_image(id){
        $.post("<?=BASE_URL;?>source/ajax.php?action=delete_image", {id: id});
        $("#img_container_" + id).hide();
    }
</script>
<? include "templates/footer.php"; ?>